package info.xiaolong.exampleservice.controller;

import info.xiaolong.exampleservice.model.document.EsTestDoc;
import info.xiaolong.exampleservice.service.IEsTestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname EsTestController
 * @Description TODO
 * @Date 2022/4/27 13:42
 * @Created by Qinger
 */
@Controller
@RequestMapping("/esTest")
@Api(tags = "ES测试类")
public class EsTestController {
    @Resource
    private IEsTestService esTestService ;

    @ApiOperation(value = "创建索引")
    @PostMapping("/createIndex")
    public ResponseEntity createIndex(@RequestParam String index) throws IOException {
        return ResponseEntity.ok(esTestService.createIndex(index));
    }

    @ApiOperation(value = "删除索引")
    @PostMapping("/deleteIndex")
    public ResponseEntity deleteIndex(@RequestParam String index) throws IOException {
        return ResponseEntity.ok(esTestService.deleteIndex(index));
    }

    @ApiOperation(value = "保存文档")
    @PostMapping("/save")
    public ResponseEntity save(@RequestBody EsTestDoc doc,@RequestParam  String index) throws IOException {
        return ResponseEntity.ok(esTestService.save(doc,index));
    }

    @ApiOperation(value = "保存文档(自动生成id)")
    @PostMapping("/saveAuto")
    public ResponseEntity saveAuto(@RequestBody EsTestDoc doc,@RequestParam  String index) throws IOException {
        return ResponseEntity.ok(esTestService.saveAuto(doc,index));
    }

    @ApiOperation(value = "删除文档")
    @PostMapping("/deleteDoc")
    public ResponseEntity deleteDoc(@RequestParam String id,@RequestParam  String index) throws IOException {
        return ResponseEntity.ok(esTestService.deleteDoc(id,index));
    }

    @ApiOperation(value = "更新文档")
    @PostMapping("/updateDoc")
    public ResponseEntity updateDoc(@RequestBody EsTestDoc doc,@RequestParam  String index) throws IOException {
        return ResponseEntity.ok(esTestService.updateDoc(doc,index));
    }

    @ApiOperation(value = "根据ID获取文档")
    @PostMapping("/getById")
    public ResponseEntity getById(@RequestParam  String index,@RequestParam  String id) throws IOException {
        return ResponseEntity.ok(esTestService.getById(index,id));
    }

    @ApiOperation(value = "根据ID获取文档(返回指定实体)")
    @PostMapping("/getById/class")
    public ResponseEntity getById4Class(@RequestParam  String index,@RequestParam  String id) throws IOException {
        return ResponseEntity.ok(esTestService.getById4Class(index,id));
    }

    @ApiOperation(value = "根据ID判断文档是否存在 ")
    @PostMapping("/ifDocExists")
    public ResponseEntity ifDocExists(@RequestParam  String index,@RequestParam  String id) throws IOException {
        return ResponseEntity.ok(esTestService.ifDocExists(index,id));
    }

    @ApiOperation(value = "批量插入 随机id")
    @PostMapping("/batchSaveDoc")
    public ResponseEntity batchSaveDoc(@RequestParam  String index,@RequestBody ArrayList<EsTestDoc> list) throws IOException {
        return ResponseEntity.ok(esTestService.batchSaveDoc(index,list));
    }






}
